package logger

import (
	"log"

	"github.com/lestrrat-go/fluent-client"

	"mychat/internal/conf"
)

type fluentdLogger struct {
	baselogger
	client fluent.Client
}

func NewFluentLogger(lc *conf.Logger) Logger {
	fluentClient, err := fluent.New(fluent.WithAddress(lc.Fluentd.Addr))
	if err != nil {
		panic(err)
	}

	level := convertFromString(lc.Level)
	l := &fluentdLogger{
		baselogger: baselogger{
			tag:      LoggerTagFluentd,
			logLevel: level,
		},
		client: fluentClient,
	}
	l.Logger = l
	return l
}
func (l *fluentdLogger) WithCustomTag(tag LoggerTag) Logger {
	nl := *l
	nl.tag = tag
	nl.Logger = &nl
	return &nl
}

func (l *fluentdLogger) WithCustomStrTag(tagstr string) Logger {
	return l.WithCustomTag(LoggerTag(tagstr))
}

func (l *fluentdLogger) LogEntry(logEntry map[string]any) {
	if l.client == nil {
		return
	}
	err := l.client.Post(string(l.tag), logEntry)
	if err != nil {
		log.Printf("fluentd log request post error||err=%v", err)
	}
}
